import pygame, sys, random
pygame.init()
pygame.key.set_repeat(100, 25)

enemy_horiz_speed = 5
enemy_vert_speed = 0

screen = pygame.display.set_mode([800,600])
screen.fill([147,180,109])

self.E_rect= pygame.Rect(100,200,20,22)


class Player:
    def __init__(self, speed):
        self.rect = pygame.Rect(200,300,11,29)
        self.speed = speed
        self.points = 0 
        self.color = [211,123,200]

    def paint(self, screen):
        pygame.draw.rect(screen, self.color, self.rect, 0)
        
    def move(self, screen, direction):
        if direction == "up":
            self.rect.top -= self.speed
            if self.rect.top < 0:
                self.rect.top = 0
        elif direction == "down":
            self.rect.top += self.speed
            if self.rect.bottom > screen.get_height():
                self.rect.bottom = screen.get_height()            
        elif direction == "left":
            self.rect.left -= self.speed
            if self.rect.left < 0:
                self.rect.left = 0
        elif direction == "right":
            self.rect.left += self.speed
            if self.rect.right > screen.get_width():
                self.rect.right = screen.get_width()                
class EnemyBrain:
    def __init__(self,E_speed):
        self.E_speed = speed
        self.E_rect =  (100,200,20,22)
        self.E_color = [100,50,25]
        
    def enemy_paint (self,screen):
        pygame.draw.rect(screen,self.E_color,self.E_rect,0)
    def enemy_move(self,screen,direction):
        if self.E_rect.right > screen.get_width():
            self.E_rect.right = screen.get_width()
            self.E_speed = self.E_speed* -1 
        elif self.E_rect.left < 0:
            self.E_rect.left = 0
            self.E_speed = self.E_speed * -1 
        elif self.E_rect.bottom > screen.get_height():
            self.E_rect.bottom = screen.get_height()
            self.E_speed = self.E_speed * -1 
        elif self.E_rect.top < 0: 
            self.E_rect.top = 0
            self.E_speed = self.E_speed * -1
        elif random.randint(0,20) < 2:
            self.E_speed = random.randint(-10,11)
                    
        



player1 = Player(7)

font = pygame.font.Font(None, 60)
score_text = font.render(str(player1.points), 1, (20, 30, 10))
textpos = [10, 11]

def paintGame():
    global player1,self.E_rect,screen
    screen.fill([147,180,109])
    player1.paint(screen)
    
    pygame.draw.rect(screen, [100,50,25], self.E_rect, 0)
    screen.blit(score_text, textpos)
    pygame.display.flip()

def activateEnemyBrain():
    global player1, self.E_rect, enemy_horiz_speed, enemy_vert_speed, score_text, font

    if self.E_rect.right > screen.get_width():
        self.E_rect.right = screen.get_width()
        enemy_horiz_speed = enemy_horiz_speed * -1 
    elif self.E_rect.left < 0:
        self.E_rect.left = 0
        enemy_horiz_speed = enemy_horiz_speed * -1 
    elif self.E_rect.bottom > screen.get_height():
        self.E_rect.bottom = screen.get_height()
        enemy_vert_speed = enemy_vert_speed * -1 
    elif self.E_rect.top < 0: 
        self.E_rect.top = 0
        enemy_vert_speed = enemy_vert_speed * -1
    elif random.randint(0,20) < 2:
        enemy_vert_speed = random.randint(-10,11)
        enemy_horiz_speed = random.randint(-10,11)
        
    if player1.rect.colliderect(self.E_rect):
        player1.points += 1 
        score_text = font.render(str(player1.points), 1, (20, 30, 10))
        
    self.E_rect.left += enemy_horiz_speed
    self.E_rect.top += enemy_vert_speed
       
    
gameIsRunning = True
while gameIsRunning:

    EnemyBrain()
    
    for event in  pygame.event.get():
        if event.type == pygame.QUIT:
           gameIsRunning = False
            
        elif event.type == pygame.KEYDOWN:
            
            if event.key == pygame.K_UP:
                player1.move(screen, "up")            
            elif event.key == pygame.K_DOWN:
                player1.move(screen, "down")          
            elif event.key == pygame.K_RIGHT:
                player1.move(screen, "right")            
            elif event.key == pygame.K_LEFT:
                player1.move(screen, "left")
                     
            if player1.rect.colliderect(self.E_rect):
                 player1.points += 1
                 score_text = font.render(str(player1.points), 1, (20, 30, 10))                          
    
    paintGame()
    pygame.time.delay(20)
    
pygame.quit()
            